package com.example.xiaxiamusicplayer.utils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.example.xiaxiamusicplayer.bean.Mp3;

/** 手机里面Mp3管理类 */
public class Mp3Manage {

	public static List<Mp3> getMp3(Context context) {
		ContentResolver mResolver = context.getContentResolver();
		List<Mp3> mp3List = new ArrayList<Mp3>();
		// 获取系统数据库音乐文件
		Cursor cursor = mResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		while (cursor.moveToNext()) {
			Mp3 mp3 = new Mp3();
			mp3.name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));// 名称
			mp3.time = toTime(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)));// 时间
			mp3.artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));// 演唱者
			mp3.size = FormetFileSize(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));
			mp3.path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));// 歌曲路径
			mp3.album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));// 图片专辑名

			long songid = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); // 音乐id
			long albumid = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));// 专辑id
			mp3.uri = getMusicBitemp(songid, albumid);

			int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); // 是否为音乐
			if (isMusic != 0) {// 是音乐才添加
				mp3List.add(mp3);
			}
		}
		cursor.close();
		return mp3List;
	}

	/**
	 * 时间格式转换
	 *
	 * @param time
	 * @return
	 */
	@SuppressLint("DefaultLocale")
	public static String toTime(long time) {
		time /= 1000;
		int minute = (int) time / 60;
		// int hour = minute / 60;
		int second = (int) time % 60;
		minute %= 60;
		return String.format("%02d:%02d", minute, second);
	}

	/**
	 * 转换文件大小
	 *
	 * @param fileS
	 * @return
	 */
	private static String FormetFileSize(long fileS) {
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSizeString = "";
		if (fileS < 1024) {
			fileSizeString = df.format((double) fileS) + "B";
		} else if (fileS < 1048576) {
			fileSizeString = df.format((double) fileS / 1024) + "K";
		} else if (fileS < 1073741824) {
			fileSizeString = df.format((double) fileS / 1048576) + "M";
		} else {
			fileSizeString = df.format((double) fileS / 1073741824) + "G";
		}
		return fileSizeString;
	}

	/**
	 * 将MP3里图片读取出来
	 *
	 * @param songid
	 * @param albumid
	 * @return
	 */
	private static Uri getMusicBitemp(long songid, long albumid) {
		Uri uri = null;
		// 专辑id和歌曲id小于0说明没有专辑、歌曲，并抛出异常
		if (albumid < 0 && songid < 0) {
			throw new IllegalArgumentException("Must specify an album or a song id");
		}
		if (albumid < 0) {
			uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");
		} else {
			uri = ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), albumid);
		}
		return uri;
	}
}
